@inherits Umbraco.Web.Macros.PartialViewMacroPage

@* Render the sitemap by passing the root node to the traverse helper *@
<div class="sitemap">
    @Traverse(CurrentPage.AncestorOrSelf())
</div>

@* Helper method to travers through all descendants *@
@helper Traverse(dynamic node)
{
    @* Update the level to reflect how deep you want the sitemap to go *@
    var maxLevelForSitemap = 4;
    
    @* Select visible children *@
    var items = node.Children.Where("Visible").Where("Level <= " + maxLevelForSitemap);

    @* If any items are returned, render a list *@
    if (items.Any())
    {
        <ul>
            @foreach (var item in items)
            {
                <li class="level-@item.Level">
                    <a href="@item.Url">@item.Name</a>

                    @* Run the traverse helper again *@
                    @Traverse(item)
                </li>
            }
        </ul>
    }
}